package com.kzd.gtgame.main.mine

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.entity.GameBean
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.game.adapter.Game1Adapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_transfer.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class MineTransferActivity : OwnerViewModeActivity<MineViewModel>() {

    private val mGame1Adapter = Game1Adapter<GameBean>(Game1Adapter.Type.GAME_LIST_TRANSFER)

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_transfer

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMineTransferBar)
    }

    override fun initView() {

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mGame1Adapter

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.mineTransferList(isLoadMore = false)
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.mineTransferList(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.mineTransferList()
    }

    override fun initViewMode() {
        viewModel.transferListLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mGame1Adapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGame1Adapter.addData(data)
            } else {
                mGame1Adapter.setHeaderView(createViewImage())
                mGame1Adapter.setNewInstance(data)
            }
        })
    }

    private fun createViewImage(): View = ImageView(this as Context).apply {
        layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        scaleType = ImageView.ScaleType.FIT_XY
        setImageResource(R.mipmap.ic_transfer_game_top)
    }
}